package com.zj.web.controller;

import com.zj.model.Product;
import com.zj.service.ProductService;
import com.zj.web.bean.ProductDTO;
import com.zj.web.bean.ResponseResult;
import io.swagger.v3.oas.annotations.Operation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.math.BigDecimal;
import java.util.List;

@RestController
@RequestMapping("/products")
@RefreshScope //开启动态刷新
public class ProductController {
    @Autowired
    private ProductService productService;

    @PostMapping("/add")
    @PreAuthorize("hasRole('ADMIN')")  //只有拥有‘ADMIN’角色的用户才能访问该接口
    @Operation(summary = "添加商品",description = "添加商品")
    public ResponseEntity<ResponseResult> addProduct(@RequestParam("categoryId") Long categoryId,
                                                     @RequestParam("productName") String productName,
                                                     @RequestPart("productImgFile") MultipartFile productImgFile,
                                                     @RequestParam("description") String description,
                                                     @RequestParam("price") Double price,
                                                     @RequestParam("originalPrice") Double originalPrice){
        ProductDTO productDTO = new ProductDTO();
        productDTO.setCategoryId(categoryId);
        productDTO.setProductName(productName);
        productDTO.setProductImgFile(productImgFile);
        productDTO.setDescription(description);
        productDTO.setPrice(price);
        productDTO.setOriginalPrice(originalPrice);
        productService.addProduct(productDTO);
        return ResponseEntity.ok(ResponseResult.builder().code(1).msg("添加成功").data(productDTO).build());
    }

    //根据产品id修改产品状态
    @PutMapping("/status/{productId}/{productStatus}")
    @PreAuthorize("hasRole('ADMIN')")  //只有拥有‘ADMIN’角色的用户才能访问该接口
    @Operation(summary = "根据产品ID修改产品状态",description = "根据产品ID修改产品状态")
    public ResponseResult updateProductStatus(@PathVariable("productId") Long productId,
                                              @PathVariable("productStatus") Integer productStatus){
        ProductDTO result = productService.updateProductStatus(productId, productStatus);
        return ResponseResult.builder().code(1).msg("修改成功").data(result).build();
    }

    //根据产品id修改产品信息
    @PutMapping("/editProduct/{productId}")
    @PreAuthorize("hasRole('ADMIN')")  //只有拥有‘ADMIN’角色的用户才能访问该接口
    @Operation(summary = "根据产品ID修改产品信息",description = "根据产品ID修改产品信息")
    public ResponseResult updateProduct(@PathVariable("productId") Long productId,
                                        @RequestParam("productName") String productName,
                                        @RequestPart("productImgFile") MultipartFile productImgFile,
                                        @RequestParam("description") String description,
                                        @RequestParam("price") Double price){
        ProductDTO productDTO = new ProductDTO();
        productDTO.setProductId(productId);
        productDTO.setProductName(productName);
        productDTO.setProductImgFile(productImgFile);
        productDTO.setDescription(description);
        productDTO.setPrice(price);
        ProductDTO result = productService.updateProduct(productDTO);
        return ResponseResult.builder().code(1).msg("修改成功").data(result).build();
    }
}
